#include "quaternion.h"

Quaternion::Quaternion()
{
    a=b=c=d=0;
}

float * Quaternion::RotationMatrix()
{
    float * matriceRotation = new float[16];

    float xx      =  a * a;
    float xy      =  a * b;
    float xz      =  a * c;
    float xw      =  a * d;

    float yy      = b * b;
    float yz      = b * c;
    float yw      = b * d;

    float zz      = c * c;
    float zw      = c * d;

    matriceRotation[0]  = 1 - 2 * ( yy + zz );
    matriceRotation[1]  =     2 * ( xy - zw );
    matriceRotation[2]  =     2 * ( xz + yw );

    matriceRotation[4]  =     2 * ( xy + zw );
    matriceRotation[5]  = 1 - 2 * ( xx + zz );
    matriceRotation[6]  =     2 * ( yz - xw );

    matriceRotation[8]  =     2 * ( xz - yw );
    matriceRotation[9]  =     2 * ( yz + xw );
    matriceRotation[10] = 1 - 2 * ( xx + yy );

    matriceRotation[3]  = matriceRotation[7] = matriceRotation[11] = matriceRotation[12] = matriceRotation[13] = matriceRotation[14] = 0;
    matriceRotation[15] = 1;

    return matriceRotation;

}

Quaternion Quaternion::Norme()
{
    Quaternion qres;

    qres.a = pow(a,2) + pow(b,2) + pow(c,2) + pow(d,2);
    qres.b = 0;
    qres.c = 0;
    qres.d = 0;

    return qres;
}

Quaternion Quaternion::operator *(const Quaternion & aQuaternion)
{
    Quaternion qres;
    qres.a = a*aQuaternion.a - b*aQuaternion.b - c*aQuaternion.c - d*aQuaternion.d;
    qres.b = a*aQuaternion.b + b*aQuaternion.a + c*aQuaternion.d - d*aQuaternion.c;
    qres.c = a*aQuaternion.c + c*aQuaternion.a + d*aQuaternion.b - b*aQuaternion.d;
    qres.d = a*aQuaternion.d + d*aQuaternion.a + b*aQuaternion.c - c*aQuaternion.b;
    return qres;
}

Quaternion Quaternion::Conjuge()
{
    Quaternion qres;

        qres.a = a;
        qres.b = -b;
        qres.c = -c;
        qres.d = -d;

    return qres;
}


void Quaternion::Print()
{
    printf("%f,",a);
    printf("%f,",b);
    printf("%f,",c);
    printf("%f\n",d);
}

